package com.elong.android.specialhouse.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.elong.android.specialhouse.R;


/**
 * Created by cheng.wang on 2015/8/11.
 */

/**
 * @author lin.chai
 * @date 2015年8月18日 下午3:13:13
 */
public class AddSubView extends FrameLayout implements View.OnClickListener {

    public static final int DEFAULT_MAX = 15;
    private FrameLayout mRootView;
    private ImageView mPlus;
    private ImageView mMinus;

    public EditText getCountEditText() {
        return mCountEditText;
    }

    private ClearableEditText mCountEditText;
    private TextView mHintText;
    private int mCount = 1;
    /**
     * 默认最小数为1,可通过setMin方法或者在布局文件中使用app:min=""设置
     */
    private int mMin = 1;
    /**
     * 默认最大数为15,可通过setMax方法或者在布局文件中使用app:max=""设置
     */
    private int mMax = DEFAULT_MAX;
    /**
     * 输入框提示,可通过setHint方法或者在布局文件中使用app:asvHint=""设置
     */
    private String mHint = null;

    public AddSubView(Context context) {
        super(context);
        init(context, null);
    }

    public AddSubView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mRootView = (FrameLayout) View.inflate(context, R.layout.custom_add_sub_view, this);
        mPlus = (ImageView) mRootView.findViewById(R.id.iv_num_plus);
        mMinus = (ImageView) mRootView.findViewById(R.id.iv_num_minus);
        mCountEditText = (ClearableEditText) mRootView.findViewById(R.id.et_count);
        mHintText = (TextView) mRootView.findViewById(R.id.tv_hint);
        if (attrs != null && getContext() != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AddSubView);
            if (typedArray != null) {
                mMax = typedArray.getInt(R.styleable.AddSubView_youfang_max, 15);
                mMin = typedArray.getInt(R.styleable.AddSubView_youfang_min, 1);
                setHint(typedArray.getString(R.styleable.AddSubView_youfang_asvHint));
                typedArray.recycle();
            }
        }
        mPlus.setOnClickListener(this);
        mMinus.setOnClickListener(this);

        // 默认加号为灰色，加号为亮色
        mPlus.setImageResource(R.drawable.icon_orange_plus);
        mMinus.setImageResource(R.drawable.icon_gray_minus);
        mMinus.setClickable(false);

        // 数量默认为最小值
        mCount = mMin;
        mCountEditText.setText(mCount + "");

        mCountEditText.setEnabled(false);
        mCountEditText.setFocusable(false);
        //默认状态设置
        if (mCount >= mMax) {
            mCount = mMax;
            mPlus.setClickable(false);
            mPlus.setImageResource(R.drawable.icon_gray_plus);
        }

        // 如果设置了提示，则显示提示
        if (mHint != null) {
            mCountEditText.setHint(mHint);
        }
    }

    @Override
    public void onClick(View v) {
        String tempCount = mCountEditText.getText().toString().trim();
        if (TextUtils.isEmpty(tempCount)) {
            return;
        }
        if (tempCount.contains("+")) {
            mCount = DEFAULT_MAX + 1;
        } else {
            try {
                mCount = Integer.valueOf(tempCount);
            } catch (NumberFormatException e) {
                mCount = DEFAULT_MAX + 1;
            }
        }
        switch (v.getId()) {
            case R.id.iv_num_plus:
                // 点击加号
                mCount++;
                if (mMax >= DEFAULT_MAX) {
                    if (mCount == DEFAULT_MAX + 1) {
                        mCountEditText.setText(DEFAULT_MAX + "+");
                        mPlus.setClickable(false);
                        mPlus.setImageResource(R.drawable.icon_gray_plus);
                        return;
                    }
                } else {
                    if (mCount >= mMax) {
                        mCount = mMax;
                        mPlus.setClickable(false);
                        mPlus.setImageResource(R.drawable.icon_gray_plus);
                        mCountEditText.setText(mCount + "");
                        return;
                    }
                }
                mCountEditText.setText(mCount + "");
                if (!mMinus.isClickable()) {
                    mMinus.setClickable(true);
                    mMinus.setImageResource(R.drawable.icon_orange_minus);
                }
                break;
            case R.id.iv_num_minus:
                // 点击减号
//                if (mCount >= mMax) {
//                    mPlus.setClickable(false);
//                    mPlus.setImageResource(R.drawable.icon_gray_plus);
//                }else if (!mPlus.isClickable() && mCount < mMax) {
//                    mPlus.setClickable(true);
//                    mPlus.setImageResource(R.drawable.icon_orange_plus);
//                }
                if (!mPlus.isClickable() && mCount < mMax) {
                    mPlus.setClickable(true);
                    mPlus.setImageResource(R.drawable.icon_orange_plus);
                }
                mCount--;
                if (mCount <= mMin) {
                    mCount = mMin;
                    mMinus.setClickable(false);
                    mMinus.setImageResource(R.drawable.icon_gray_minus);
                    mCountEditText.setText(mCount + "");
                    return;
                }
                mCountEditText.setText(mCount + "");
                break;

            default:
                break;
        }
    }

    /**
     * 输入法开关
     * 设置为true表示 输入法可用
     *
     * @param isEnable
     */
    public void setEditTextEnable(boolean isEnable) {
        mCountEditText.setEnabled(isEnable);
        mCountEditText.setFocusable(isEnable);
        mCountEditText.setFocusableInTouchMode(isEnable);
    }

    public int getCount() {
        String tempCount = mCountEditText.getText().toString().trim();
        if (TextUtils.isEmpty(tempCount)) {
            return -1;
        }
        if (tempCount.contains("+")) {
            return DEFAULT_MAX + 1;
        }
        try {
            mCount = Integer.valueOf(tempCount);
        } catch (NumberFormatException e) {
            mCount = DEFAULT_MAX + 1;
        }
        return mCount;
    }

    public void setCount(int mCount) {
        this.mCount = mCount;
        if (mCount == mMin) {
            mMinus.setClickable(false);
            mPlus.setClickable(true);
            mMinus.setImageResource(R.drawable.icon_gray_minus);
            mPlus.setImageResource(R.drawable.icon_orange_plus);
        } else if (mCount > mMin && mCount <= mMax) {
            mMinus.setClickable(true);
            mPlus.setClickable(true);
            mMinus.setImageResource(R.drawable.icon_orange_minus);
            mPlus.setImageResource(R.drawable.icon_orange_plus);
        } else if (mCount > mMax) {
            mMinus.setClickable(true);
            mPlus.setClickable(false);
            mMinus.setImageResource(R.drawable.icon_orange_minus);
            mPlus.setImageResource(R.drawable.icon_gray_plus);
            mCountEditText.setText(mMax + "+");
            return;
        }
        mCountEditText.setText(mCount + "");
    }

    /**
     * 设置最小值
     *
     * @param Min
     */
    public void setMin(int Min) {
        this.mMin = Min;
    }


    /**
     * 设置最大值
     *
     * @param Max
     */
    public void setMax(int Max) {
        this.mMax = Max;
    }

    /**
     * 设置输入提示
     *
     * @param Hint
     */
    public void setHint(String Hint) {
        this.mHint = Hint;
    }
}
